package map.android.baidu.carpool.reservation;

import java.util.ArrayList;

import com.baidu.components.api.extra.APIProxy;

import android.app.Activity;
import android.app.Dialog;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import map.android.baidu.carpool.R;
import map.android.baidu.carpool.model.AirportTransCity;
import map.android.baidu.carpool.widget.wheel.OnWheelChangedListener;
import map.android.baidu.carpool.widget.wheel.WheelView;

public class CarCitySelectDialog extends Dialog {
	private Activity mActivity;
	
	private ArrayList<AirportTransCity> mAllCityList;
	
	public static final boolean VISIBLE = true;
	public static final boolean GONE = false;
	
	private WheelView cityWheelView = null;
	private CitysWheelAdapter cityAdapter = null;

	private OnSelectCityListener citySelectListener ;
	private static final double SCALE = 1.0;

	public interface OnSelectCityListener{
		void citySelected(AirportTransCity city);
	}
	

	public CarCitySelectDialog(Activity activity,  int style,OnSelectCityListener citySelectListener,ArrayList<AirportTransCity> mAllCityList) {
		super(activity, style);
		this.mActivity = activity;
		this.mAllCityList = mAllCityList;
		this.citySelectListener = citySelectListener;
		init();
	}

	@SuppressWarnings("deprecation")
	private void init() {
		LayoutInflater inflater = APIProxy.res().getInflater();
		View view = inflater.inflate(R.layout.car_dialog_city_select_layout, null);

		setContentView(view);

		WindowManager windowManager = mActivity.getWindowManager();
		Display display = windowManager.getDefaultDisplay();

		Window window = getWindow();
		WindowManager.LayoutParams attributes = window.getAttributes();
		attributes.gravity = Gravity.BOTTOM;
		attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
		attributes.width = (int) (display.getWidth() * SCALE);
		window.setAttributes(attributes);

		initViewUseNew(view);
	}

	private void initViewUseNew(View view) {
		try {
			LinearLayout wheelContainerView = (LinearLayout) view.findViewById(R.id.car_wheel_container);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.weight = 0.33f;

			cityAdapter = new  CitysWheelAdapter(mActivity);
			cityAdapter.setData(mAllCityList);
			cityWheelView = new WheelView(mActivity);
			wheelContainerView.addView(cityWheelView, lp);
			cityWheelView.setCurrentItem(0);
			cityWheelView.setViewAdapter(cityAdapter);
			cityAdapter.setItemResource(R.layout.car_wheel_text_item);
			cityAdapter.setItemTextResource(R.id.car_reservation_wheelview_text);
			cityWheelView.addChangingListener(new OnWheelChangedListener() {
				@Override
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					
				}
			});
			TextView okTextView = (TextView) view.findViewById(R.id.car_reservation_wheel_ok_textview);
			okTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (citySelectListener != null) {
						AirportTransCity city = cityAdapter.getItem(cityWheelView.getCurrentItem());
						citySelectListener.citySelected(city);
					}
					dismiss();
				}
			});
			TextView cancelTextView = (TextView) view.findViewById(R.id.car_reservation_wheel_cancel_textview);
			cancelTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
